﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace produccion.clases
{
    public class CLSREFCLCP:ObservableObject
    {
        MDOgeneral mod = new MDOgeneral();

        #region propiedades
        private string codclc_rxc, codref_rxc, codcol_rxc;
        public string CODCLC_RXC
        {
            get { return codclc_rxc; }
            set {
                if (codclc_rxc != value) { CAMBIO = true; }
                codclc_rxc = value;
            RaisePropertyChangedEvent("CODCLC_RXC");
            }
        }
        public string CODREF_RXC
        {
            get { return codref_rxc; }
            set {
                if (codref_rxc != value) { CAMBIO = true; } 
                codref_rxc = value;
            RaisePropertyChangedEvent("CODREF_RXC");
            }
        }
        public string CODCOL_RXC
        {
            get { return codcol_rxc; }
            set {
                if (codcol_rxc != value) { CAMBIO = true; } 
                codcol_rxc = value;
            RaisePropertyChangedEvent("CODCOL_RXC");
            }
        }
        private Boolean _CAMBIO = false;
        public Boolean CAMBIO
        {
            get { return _CAMBIO; }
            set
            {
                _CAMBIO = value;
                RaisePropertyChangedEvent("CAMBIO");
            }
        }
        private Boolean _EXISTE = false;
        public Boolean EXISTE
        {
            get { return _EXISTE; }
            set
            {
                _EXISTE = value;
                RaisePropertyChangedEvent("EXISTE");
            }
        }

        public CLSMAEREF referencia = new CLSMAEREF();
        #endregion

        #region metodos basicos

        //Adicionado David Arango 26-06-2012: Consulta de colecciones para una referencia
        public DataTable ColeccionesPorRef()
        {
            try
            {
                string query = "select codclc_clc,nomclc_clc,codclc_rxc from (select codclc_clc,nomclc_clc from maeclcp) A  LEFT OUTER JOIN (select DISTINCT  codclc_rxc,codref_rxc from refclcp where codref_rxc='" + codref_rxc + "') B ON codclc_clc= codclc_rxc Order by nomclc_clc";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void LimpiarValores()
        {
            CODCLC_RXC  = "";
            CODREF_RXC = "";
            CODCOL_RXC  = "";
        }

        public Boolean ExisteColeccion()
        {
            try
            {
                string query = "select codclc_rxc, codref_rxc, codcol_rxc from refclcp where codclc_rxc='" + CODCLC_RXC + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Consultar()
        {
            try
            {
                string query = "select codref_rxc || codcol_rxc as Codigo, nomref_ref as Nombre, nomcol_col as Color from refclcp inner join maeref on codref_rxc=codref_ref inner join maecol on codcol_rxc=codcol_col where codclc_rxc='" + CODCLC_RXC + "'";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Eliminar()
        {
            try
            {
                string query = "delete from refclcp where codclc_rxc='" + CODCLC_RXC + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int EliminarRefCol()
        {
            try
            {
                string query = "delete from refclcp where codclc_rxc='" + CODCLC_RXC + "' and codref_rxc='" + CODREF_RXC + "' and codcol_rxc='" + CODCOL_RXC + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Insertar()
        {
            try
            {
                string query = "insert into refclcp (codclc_rxc, codref_rxc, codcol_rxc) values ('" + CODCLC_RXC + "','" + CODREF_RXC + "','" + CODCOL_RXC + "')";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Boolean ExisteColor(string Color)
        {
            try
            {
                string query = "select * from maecol where codcol_col='" + Color + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int insertarRefCole()
        {
            try
            {
                string query = "insert into REFCLCP (CODREF_RXC,CODCLC_RXC,CODCOL_RXC) values ('" + CODREF_RXC + "','" + CODCLC_RXC + "','" + CODCOL_RXC + "')";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        public Boolean ExisteRefCole() {

            string query = "select count(codref_rxc) from REFCLCP where (codclc_rxc='" + CODCLC_RXC + "' and codref_rxc='" + CODREF_RXC + "' and codcol_rxc='" + CODCOL_RXC + "')";
            DataTable val = mod.consulta(query);
            if (val.Rows[0][0].ToString().Equals("0")){
            return false;
            }
            return true;


        }
        #endregion

    }
}
